package cn.mickey.exchange.service;

import cn.mickey.exchange.domain.model.AddressModel;
import cn.mickey.exchange.mapper.AddressMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressService extends ServiceImpl<AddressMapper, AddressModel> {

    /**
     * 根据用户id获取当前用户的所有收货地址
     */
    public List<AddressModel> getAddressListByUserId(Long userId) {
        return baseMapper.selectList(new LambdaQueryWrapper<AddressModel>()
                .eq(AddressModel::getUserId, userId));
    }

    /**
     * 是否存在默认地址
     *
     * @param userId
     * @return
     */
    public boolean isAddressDefault(Long userId) {
        return baseMapper.selectCount(new LambdaQueryWrapper<AddressModel>()
                .eq(AddressModel::getDefaultFlag, true)
                .eq(AddressModel::getUserId, userId)) == 1;
    }

    /**
     * 将第一个地址设置成默认地址
     *
     * @param userId
     */
    public void setFirstAddressToDefault(Long userId) {
        List<AddressModel> addressList = getAddressListByUserId(userId);
        //如果不存在默认地址,要将第一个地址设为默认地址
        Long addressId = addressList.get(0).getAddressId();
        baseMapper.update(null, new LambdaUpdateWrapper<AddressModel>()
                .set(AddressModel::getDefaultFlag, true)
                .eq(AddressModel::getAddressId, addressId));
    }

    /**
     * 修改默认地址
     *
     * @param address
     * @param isDefaultAddress
     */
    public void setDefaultAddress(AddressModel address, Boolean isDefaultAddress) {
        LambdaUpdateWrapper<AddressModel> luw = new LambdaUpdateWrapper();
        luw.set(AddressModel::getDefaultFlag, isDefaultAddress)
                .eq(AddressModel::getUserId, address.getUserId());
        baseMapper.update(null, luw);
    }


    /**
     * 添加一个新收货地址
     *
     * @param address
     * @return
     */
    public boolean addAddress(AddressModel address) {
        //默认地址应该只有一个,如果存在需要将其他地址修改
        if (address.getDefaultFlag()) {
            setDefaultAddress(address, false);
        }
        return baseMapper.insert(address) == 1;

    }

    /**
     * 删除当前地址
     *
     * @param addressModel
     * @return
     */
    public boolean deleteAddress(AddressModel addressModel) {
        return baseMapper.delete(new LambdaQueryWrapper<AddressModel>()
                .eq(AddressModel::getAddressId, addressModel.getAddressId())
                .eq(AddressModel::getUserId, addressModel.getUserId())) == 1;
    }

    /**
     * 修改当前地址
     *
     * @param addressModel
     * @return
     */
    public boolean updateAddress(AddressModel addressModel) {
        LambdaUpdateWrapper<AddressModel> luw = new LambdaUpdateWrapper();
        luw.eq(AddressModel::getUserId, addressModel.getUserId())
                .eq(AddressModel::getAddressId, addressModel.getAddressId());
        AddressModel newAddress = new AddressModel();
        newAddress.setConsigneeName(addressModel.getConsigneeName())
                .setConsigneePhone(addressModel.getConsigneePhone())
                .setProvinceName(addressModel.getProvinceName())
                .setCityName(addressModel.getCityName())
                .setRegionName(addressModel.getRegionName())
                .setDetailAddress(addressModel.getDetailAddress());
        if (addressModel.getDefaultFlag()) {
            //如果存在默认地址要先修改
            setDefaultAddress(addressModel, false);
            newAddress.setDefaultFlag(true);
        } else {
            newAddress.setDefaultFlag(false);
        }
        return baseMapper.update(newAddress, luw) == 1;
    }
}
